/**
 * 
 */
package com.lehecai.ucenter.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang.StringUtils;

import com.lehecai.core.util.CharsetConstant;
import com.lehecai.core.util.CoreStringUtils;

/**
 * @author qatang
 *
 */
public class AuthorizeSignUtils {
	public static String generateAuthorizeSign(Map<String, String> params, String secret) {
		List<String> keyList = new ArrayList<String>();
		for (String key :params.keySet()) {
			keyList.add(key);
		}
		Collections.sort(keyList);
		List<String> signList = new ArrayList<String>();
		for (String key : keyList) {
			String value = params.get(key);
			if (!StringUtils.isEmpty(value)) {
				StringBuffer sb = new StringBuffer();
				sb.append(key).append("=").append(value);
				signList.add(sb.toString());
			}
		}
		signList.add(secret);	
		return CoreStringUtils.md5(StringUtils.join(signList, "&"), CharsetConstant.CHARSET_UTF8);
	}
	
	public static String generateRandomParam() {
		return CoreStringUtils.md5(String.valueOf(new Random().nextLong()), CharsetConstant.CHARSET_UTF8);
	}
	
}
